package com.bjsxt.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.CoinType;
import com.bjsxt.model.Result;
import com.bjsxt.service.CoinTypeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * description:
 * 币种类型控制器
 * TODO: 这里在进行权限配置的时候可能没有对应的权限，以后后期需要统一进行添加
 *
 * @author ALIENWARE
 * @version v1.0.0
 * @create 12/30/2023
 **/
@RestController
@RequestMapping("/coinTypes")
@Api(tags = "币种类型控制器")
public class CoinTypeController {

    /**
     * 币种参数业务层接口
     */
    @Autowired
    private CoinTypeService coinTypeService;

    /**
     * 条件分页查询币种类型
     *
     * @param page 分页对象<CoinType>
     * @return 统一响应对象
     */
    @GetMapping
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    @ApiOperation(value = "条件分页查询币种类型", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20"),
            @ApiImplicitParam(name = "code", value = "币种类型代码", dataType = "String", paramType = "query", example = "1234565")
    })
    public Result<Page<CoinType>> findByPage(@ApiIgnore Page<CoinType> page, String code) {
        page.addOrder(OrderItem.desc(CoinType.COL_LAST_UPDATE_TIME));
        // 调用业务层进行分页数据的查询
        Page<CoinType> coinTypePage = coinTypeService.pageByCode(page, code);
        // 返回响应数据
        return Result.success(coinTypePage);
    }

    /**
     * 根据状态获取全部的币种类型
     *
     * @param status 状态：0: 无效 1:有效
     * @return 统一响应对象
     */
    @GetMapping("/all")
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    @ApiOperation(value = "根据状态获取全部的币种类型", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态：0-无效；1-有效；", dataType = "Byte", required = true, paramType = "query", example = "0")
    })
    public Result<List<CoinType>> getAll(@NotNull Byte status) {
        // 调用业务层查询所有的币种类型
        List<CoinType> coinTypes = coinTypeService.listByStatus(status);
        // 返回币种查询的币种类型数据
        return Result.success(coinTypes);
    }

    /**
     * 增加一个币种类型
     *
     * @param coinType 币种类型添加数据对象
     * @return 统一响应对象
     */
    @PostMapping
    @PreAuthorize("hasAuthority('trade_coin_type_create')")
    @ApiOperation(value = "增加一个币种类型", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinType", value = "CoinType 的 json 数据", dataType = "String", paramType = "body", example = "{}"),
    })
    public Result<Void> add(@Validated @RequestBody CoinType coinType) {
        // 调用业务层方法进行数据的保存
        boolean flag = coinTypeService.save(coinType);
        // 如果为 true 新增成功
        if (flag) return Result.success("新增成功");
        // 如果为 false 新增失败
        return Result.fail("新增失败");
    }

    /**
     * 对一个币种类型进行更新操作
     *
     * @param coinType 币种类型修改数据对象
     * @return 统一响应对象
     */
    @PatchMapping
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    @ApiOperation(value = "修改一个币种类型", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinType", value = "CoinType 的 json 数据", dataType = "String", paramType = "body", example = "{}"),
    })
    public Result<Void> update(@Validated @RequestBody CoinType coinType) {
        // 调用业务层方法进行数据的更新
        boolean flag = coinTypeService.updateById(coinType);
        // 如果为 true 更新成功
        if (flag) return Result.success("修改成功");
        // 如果为 false 更新失败
        return Result.fail("修改失败");
    }

    /**
     * 启用/禁用一个货币类型
     *
     * @param id     "要启用/禁用货币类型的 Id
     * @param status 要设置的货币类型的状态值 1: 启用 0: 禁用
     * @return 统一响应对象
     */
    @PostMapping("/setStatus")
    @PreAuthorize("hasAuthority('admin_bank_update')")
    @ApiOperation(value = "启用/禁用一个货币类型", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要启用/禁用货币类型的 Id", dataType = "Long", required = true, paramType = "query", example = "1"),
            @ApiImplicitParam(name = "status", value = "要设置的货币类型的状态值 1: 启用 0: 禁用", dataType = "Byte", paramType = "query", example = "0")
    })
    public Result<Void> updateStatus(@NotNull Long id, Byte status) {
        // 创建更新的 coinType 对象
        CoinType coinType = new CoinType();
        // 设置 id 及需要更新的值
        coinType.setId(id);
        coinType.setStatus(status);
        // 调用业务层方法进行数据的更新
        boolean flag = coinTypeService.updateById(coinType);
        // 如果为 true 更新成功
        if (flag) return Result.success("更新成功");
        // 如果为 false 更新失败
        return Result.fail("更新失败");
    }

    /**
     * 删除货币类型
     *
     * @param ids 需要删除的货币类型 ID 集合
     * @return 统一响应对象
     */
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin_bank_delete')")
    @ApiOperation(value = "删除货币类型（可以多个）", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams(
            @ApiImplicitParam(name = "ids", value = "币种类型 ID 数组", dataType = "Integer", required = true, paramType = "query", example = "1")
    )
    public Result<Void> delete(@RequestParam("ids") @NotEmpty List<Long> ids) {
        boolean flag = coinTypeService.removeByIds(ids);
        // 如果为 true 删除成功
        if (flag) return Result.success("删除成功");
        // 如果为 false 删除失败
        return Result.fail("删除失败");
    }
}
